package com.finance.scheduler.job;

import com.finance.base.constant.BaseConstants;
import com.finance.framework.qurtz.TaskDescriptor;
import com.finance.scheduler.service.AbstractTask;
import com.finance.system.service.ITableService;
import com.finance.util.DateUtil;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by CBWL on 2017/5/17.
 */
@Component
@DisallowConcurrentExecution
public class TableGenJob implements AbstractTask{
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITableService tableService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Calendar oneMonthAfter = Calendar.getInstance();
        oneMonthAfter.roll(Calendar.MONTH,1);
        String dest = BaseConstants.SYSCONFIG_KEY_LOGISTICDATA_TABLE + DateUtil.dateFormat(new Date(oneMonthAfter.getTimeInMillis()),"yyMM");
        String dest_extend = BaseConstants.SYSCONFIG_KEY_LOGISTICS_EXTEND_TABLE + DateUtil.dateFormat(new Date(oneMonthAfter.getTimeInMillis()),"yyMM");
        String dest_log = BaseConstants.SYSCONFIG_KEY_LOGISTICERROR_TABLE + DateUtil.dateFormat(new Date(oneMonthAfter.getTimeInMillis()),"yyMM");

        String origin = BaseConstants.SYSCONFIG_KEY_LOGISTICDATA_TABLE + DateUtil.dateFormat(new Date(),"yyMM");
        String origin_extend = BaseConstants.SYSCONFIG_KEY_LOGISTICS_EXTEND_TABLE + DateUtil.dateFormat(new Date(),"yyMM");
        String origin_log = BaseConstants.SYSCONFIG_KEY_LOGISTICERROR_TABLE + DateUtil.dateFormat(new Date(),"yyMM");

        this.tableService.copyTable(origin,dest);
        this.tableService.copyTable(origin_extend,dest_extend);
        this.tableService.copyTable(origin_log,dest_log);
    }

    @Override
    public TaskDescriptor descriptor() {
        return new TaskDescriptor("月终","defaultGroup","genMonthTableTrigger","triggerGroup1","0 0 0 28 * ?",this.getClass().getCanonicalName(),"预先生成月表");
    }

}
